package org.wyn.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.wyn.mongodb.bean.User;
import org.wyn.util.FreeMarkerUtil;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 基础action类
 * @author wyn
 *
 */
public class WynActionSupport extends ActionSupport{
	
	public FreeMarkerUtil fmUtil = new FreeMarkerUtil();
	
	public FreeMarkerUtil getFmUtil() {
		return fmUtil;
	}
	public void setFmUtil(FreeMarkerUtil fmUtil) {
		this.fmUtil = fmUtil;
	}
	/**
	 * 向response中写入字符串
	 * @param str
	 */
	protected void output(String str) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 从session中获得当前登录的用户
	 * @return
	 */
	public User getLoginUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		return (User)request.getSession().getAttribute("user");
	
	}
	/**
	 * 从session中获得当前登录的用户的账号
	 * @return
	 */
	public String getLoginUserName(){
		return getLoginUser().getUserName();
	}
	public HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	private static final long serialVersionUID = 1L;
}
